﻿<style>
    #roleAuthTable + .layui-table-view .layui-table tbody tr:hover {
        background-color: transparent;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="roleEdtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="roleBtnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="roleBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="roleTable" lay-filter="roleTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="roleTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="auth">接口权限</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="roleForm">
    <form lay-filter="roleForm" class="layui-form model-form">
        <input name="roleId" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">角色名</label>
            <div class="layui-input-block">
                <input name="roleName" placeholder="请输入角色名" type="text" class="layui-input"
                       maxlength="20" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="comments" placeholder="请输入备注" class="layui-textarea" maxlength="200"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="roleFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- 接口权限 -->
<script type="text/html" id="authModel">
    <table class="layui-table" id="roleAuthTable" lay-filter="roleAuthTable"></table>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="authState">
    <input type="checkbox" lay-filter="authState" value="{{d.authority}}" lay-skin="switch"
           lay-text="ON|OFF" {{d.checked==1?'checked':''}}/>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'config', 'tableX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var tableX = layui.tableX;

        //渲染表格
        table.render({
            elem: '#roleTable',
            url: config.base_server + 'role',
            where: {
                access_token: config.getToken()
            },
            page: false,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'roleName', title: '角色名'},
                {field: 'comments', title: '备注'},
                {
                    templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#roleTableBar', title: '操作', minWidth: 200}
            ]]
        });

        // 添加按钮点击事件
        $('#roleBtnAdd').click(function () {
            showEditModel();
        });

        // 搜索按钮点击事件
        $('#roleBtnSearch').click(function () {
            var keyword = $('#roleEdtSearch').val();
            table.reload('roleTable', {where: {keyword: keyword}});
        });

        // 工具条点击事件
        table.on('tool(roleTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            } else if (obj.event === 'auth') {  // 权限管理
                showPermDialog(obj.data.roleId);
            }
        });

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('role/' + obj.data.roleId, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '360px',
                offset: '65px',
                title: data ? '修改角色' : '添加角色',
                content: $('#roleForm').html(),
                success: function () {
                    form.val('roleForm', data);
                    // 表单提交事件
                    form.on('submit(roleFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req('role', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('roleTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 权限管理
        function showPermDialog(roleId) {
            admin.open({
                type: 1,
                area: '650px',
                offset: '65px',
                title: '角色权限管理',
                content: $('#authModel').html(),
                success: function () {
                    // 渲染表格
                    table.render({
                        elem: '#roleAuthTable',
                        url: config.base_server + 'authorities',
                        where: {
                            access_token: config.getToken(),
                            roleId: roleId
                        },
                        page: false,
                        height: 400,
                        cols: [[
                            {type: 'numbers'},
                            {field: 'parentName', sort: true, title: '模块', width: 115},
                            {field: 'authorityName', sort: true, title: '接口名称', unresize: true, width: 165},
                            {field: 'authority', sort: true, title: '权限标识', unresize: true},
                            {templet: '#authState', title: '授权', unresize: true, width: 90}
                        ]],
                        done: function (res, curr, count) {
                            tableX.merges('roleAuthTable', [1], ['parentName']);
                        }
                    });

                    // 监听授权开关
                    form.on('switch(authState)', function (obj) {
                        layer.load(2);
                        admin.req('authorities/role', {
                            roleId: roleId,
                            authId: obj.value
                        }, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                                $(obj.elem).prop('checked', !obj.elem.checked);
                                form.render('checkbox');
                            }
                        }, obj.elem.checked ? 'POST' : 'DELETE');
                    });
                    // 去掉margin
                    $('#roleAuthTable').css('margin', '0');
                    $('#roleAuthTable+.layui-table-view').css('margin', '0');
                }
            });
        }

    });
</script>